package com.charactereducation.productcenter.service;

import com.charactereducation.module.vo.Product;
import com.charactereducation.dao.ProductMapper;
import com.charactereducation.tools.GenerateID;
import com.charactereducation.tools.myRunTime.MyRunTimeExcption;
import com.charactereducation.webcore.service.ServiceBase;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * Created by Baggio on 2017-7-5.
 */
public class ProductBase extends ServiceBase {
    @Autowired
   protected ProductMapper productMapper;

    protected void insertProduct(Product product) {
        product.setProductId(GenerateID.CreatetID());
        product.setCreateTime(new Date());
        productMapper.insertProduct(product);
    }

    protected void updateProduct(Product product) {

        Product product1 = productMapper.getProductByProductId(product.getProductId());
        if (product1 == null) {
            throw new MyRunTimeExcption("产品没有找到！");
        }

        if (product.getProductType() != null) {
            product1.setProductType(product.getProductType());
        }
        if (StringUtils.isNotBlank(product.getProductIntroduction())) {
            product1.setProductIntroduction(product.getProductIntroduction());
        }
        if (StringUtils.isNotBlank(product.getProductName())) {
            product1.setProductName(product.getProductName());
        }
        productMapper.updateProductById(product1);
    }

    protected Product getProductByProductId(Long productId) {
        return productMapper.getProductByProductId(productId);

    }

}
